package cn.loveq.android.widget;

import java.util.zip.Inflater;

import cn.loveq.android.R;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TextView;

/**
 * 
 * 
 * 项目名称：loveq4Weibo 类名称：WeiboGalleryViewItem 类描述：微博 Gallery的模块 创建人：李伟豪
 * 创建时间：2011-12-5 下午03:49:24
 * 
 * @version
 * 
 */
public class WeiboGalleryViewItem extends RelativeLayout {
	private static final int MENU_ALL = -1;
	private static final int MENU_PHOTO = 0;
	private static final int MENU_TOPIC = 1;
	private static final int MENU_AME = 2;
	private static final int MENU_FACE = 3;
	private static final int MENU_MORE = 4;

	private LinearLayout layout_menu;

	private ImageView home_menu_iv_photo;
	private ImageView home_menu_iv_topic;
	private ImageView home_menu_iv_ame;
	private ImageView home_menu_iv_face;
	private ImageView home_menu_iv_more;

	private PopupWindow menu_photo;
	private PopupWindow menu_topic;
	private PopupWindow menu_ame;
	private PopupWindow menu_face;
	private PopupWindow menu_more;

	private LayoutInflater inflater;
	private View menuView;

	private View layout_menu_photo;
	private View layout_menu_topic;
	private View layout_menu_ame;
	private View layout_menu_face;
	private View layout_menu_more;

	private View weibo_home;
	private TextView textView;
	private final int color_red = Color.argb(100, 200, 0, 0);

	public WeiboGalleryViewItem(Context context, int position) {
		super(context);

		this.setLayoutParams(new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT));
		this.setBackgroundColor(android.R.color.transparent);
		// textView = new TextView(context);
		// textView.setTextSize(30);
		// textView.setText("微博界面");
		// textView.setGravity(Gravity.LEFT);
		// textView.setBackgroundColor(color_red);

		inflater = LayoutInflater.from(context);
		weibo_home = inflater.inflate(R.layout.weibo_home, null);
		initUI();
		initMenu();
		setListener();
		this.addView(weibo_home, new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT));

	}

	// 初始化menu
	private void initMenu() {

		layout_menu_photo = inflater.inflate(R.layout.menu_photo, null);
		menu_photo = new PopupWindow(layout_menu_photo,
				WindowManager.LayoutParams.WRAP_CONTENT,
				WindowManager.LayoutParams.WRAP_CONTENT);

		layout_menu_topic = inflater.inflate(R.layout.menu_topic, null);
		menu_topic = new PopupWindow(layout_menu_topic,
				WindowManager.LayoutParams.WRAP_CONTENT,
				WindowManager.LayoutParams.WRAP_CONTENT);

		layout_menu_ame = inflater.inflate(R.layout.menu_ame, null);
		menu_ame = new PopupWindow(layout_menu_ame,
				WindowManager.LayoutParams.WRAP_CONTENT,
				WindowManager.LayoutParams.WRAP_CONTENT);

		layout_menu_face = inflater.inflate(R.layout.menu_face, null);
		menu_face = new PopupWindow(layout_menu_face,
				WindowManager.LayoutParams.WRAP_CONTENT,
				WindowManager.LayoutParams.WRAP_CONTENT);

		layout_menu_more = inflater.inflate(R.layout.menu_more, null);
		menu_more = new PopupWindow(layout_menu_more,
				WindowManager.LayoutParams.WRAP_CONTENT,
				WindowManager.LayoutParams.WRAP_CONTENT);

	}

	// 初始化UI
	private void initUI() {
		layout_menu = (LinearLayout) weibo_home.findViewById(R.id.layout_menu);
		home_menu_iv_photo = (ImageView) weibo_home
				.findViewById(R.id.home_menu_iv_photo);
		home_menu_iv_topic = (ImageView) weibo_home
				.findViewById(R.id.home_menu_iv_topic);
		home_menu_iv_ame = (ImageView) weibo_home
				.findViewById(R.id.home_menu_iv_ame);
		home_menu_iv_face = (ImageView) weibo_home
				.findViewById(R.id.home_menu_iv_face);
		home_menu_iv_more = (ImageView) weibo_home
				.findViewById(R.id.home_menu_iv_more);
	}

	// 关闭已开的menu id代表的menu除外
	private void closeMenu(int id) {
		if (menu_photo.isShowing() && menu_photo != null && id != MENU_PHOTO) {
			home_menu_iv_photo
					.setBackgroundResource(R.drawable.material_home_menu_photo);
			menu_photo.dismiss();
		}
		if (menu_topic.isShowing() && menu_topic != null && id != MENU_TOPIC) {
			home_menu_iv_topic
					.setBackgroundResource(R.drawable.material_home_menu_topic);
			menu_topic.dismiss();
		}
		if (menu_ame.isShowing() && menu_ame != null && id != MENU_AME) {
			home_menu_iv_ame
					.setBackgroundResource(R.drawable.material_home_menu_ame);
			menu_ame.dismiss();
		}
		if (menu_face.isShowing() && menu_face != null && id != MENU_FACE) {
			home_menu_iv_face
					.setBackgroundResource(R.drawable.material_home_menu_face);
			menu_face.dismiss();
		}
		if (menu_more.isShowing() && menu_more != null && id != MENU_MORE) {
			home_menu_iv_more
					.setBackgroundResource(R.drawable.material_home_menu_more);
			menu_more.dismiss();
		}
	}

	// 设置监听器
	private void setListener() {
		menuView = weibo_home.findViewById(R.id.layout_menu);
		home_menu_iv_photo.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 防止空指针异常
				if (menu_photo == null)
					return;
				// 如果menu在显示
				if (!menu_photo.isShowing()) {
					closeMenu(MENU_PHOTO);
					home_menu_iv_photo
							.setBackgroundResource(R.drawable.material_home_menu_photo_selected);
					menu_photo.showAtLocation(menuView, Gravity.BOTTOM
							| Gravity.CENTER_HORIZONTAL, 0,
							home_menu_iv_photo.getHeight());
				} else {
					home_menu_iv_photo
							.setBackgroundResource(R.drawable.material_home_menu_photo);
					menu_photo.dismiss();
				}
			}
		});
		home_menu_iv_topic.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (menu_topic == null)
					return;
				if (!menu_topic.isShowing()) {
					closeMenu(MENU_TOPIC);
					home_menu_iv_topic
							.setBackgroundResource(R.drawable.material_home_menu_topic_selected);
					menu_topic.showAtLocation(menuView, Gravity.BOTTOM
							| Gravity.CENTER_HORIZONTAL, 0,
							layout_menu.getHeight());
				} else {
					home_menu_iv_topic
							.setBackgroundResource(R.drawable.material_home_menu_topic);
					menu_topic.dismiss();
				}
			}
		});
		home_menu_iv_ame.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (menu_ame == null)
					return;
				if (!menu_ame.isShowing()) {
					closeMenu(MENU_AME);
					home_menu_iv_ame
							.setBackgroundResource(R.drawable.material_home_menu_ame_selected);
					menu_ame.showAtLocation(menuView, Gravity.BOTTOM
							| Gravity.CENTER_HORIZONTAL, 0,
							layout_menu.getHeight());
				} else {
					home_menu_iv_ame
							.setBackgroundResource(R.drawable.material_home_menu_ame);
					menu_ame.dismiss();
				}
			}
		});
		home_menu_iv_face.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (menu_face == null)
					return;
				if (!menu_face.isShowing()) {
					closeMenu(MENU_FACE);
					home_menu_iv_face
							.setBackgroundResource(R.drawable.material_home_menu_face_selected);
					menu_face.showAtLocation(menuView, Gravity.BOTTOM
							| Gravity.CENTER_HORIZONTAL, 0,
							layout_menu.getHeight());
				} else {
					home_menu_iv_face
							.setBackgroundResource(R.drawable.material_home_menu_face);
					menu_face.dismiss();
				}
			}
		});
		home_menu_iv_more.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (menu_more == null)
					return;
				if (!menu_more.isShowing()) {
					closeMenu(MENU_MORE);
					home_menu_iv_more
							.setBackgroundResource(R.drawable.material_home_menu_more_selected);
					menu_more.showAtLocation(menuView, Gravity.BOTTOM
							| Gravity.CENTER_HORIZONTAL, 0,
							layout_menu.getHeight());
				} else {
					home_menu_iv_more
							.setBackgroundResource(R.drawable.material_home_menu_more);
					menu_more.dismiss();
				}
			}
		});
	}

}
